package com.capgemini.rdlgae.controller

import com.capgemini.rdlgae.domain.Commande
import com.capgemini.rdlgae.domain.Entree
import com.capgemini.rdlgae.domain.Plat
import com.capgemini.rdlgae.domain.Dessert
import com.capgemini.rdlgae.domain.Utilisateur

class CommandeController {

	
	static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
	def commandeService
	def envoiMailService
	
	def envoi = {
		envoiMailService.envoiMail()
		redirect(action: "list", params: params)
	}
	def calculerTotal = {
		
		def entree
		def plat
		def dessert
		
		if ( !"null".equals(params['idEntree']))
			entree =  Entree.get( params['idEntree'] )

		if ( !"null".equals(params['idPlat']))
			plat =  Plat.get( params['idPlat'] )

		if ( !"null".equals(params['idDessert'] ))
			dessert =  Dessert.get( params['idDessert'] )
		
		def total = commandeService.calculerTotal(entree, plat, dessert)
		render (total )
	}
	
	def index = {
		redirect(action: "list", params: params)
	}
	
	def list = {
		params.max = Math.min(params.max ? params.int('max') : 10, 100)
		params.sort = "dateCommande"
		params.order = "desc"
		
		[commandeInstanceList: Commande.list(params), commandeInstanceTotal: Commande.count()]
	}
	
	def create = {
		def commandeInstance = new Commande()
		commandeInstance.properties = params
		
		def entrees = Entree.findAllByActif(true)
		def plats = Plat.findAllByActif(true)
		def desserts = Dessert.findAllByActif(true)
		def utilisateurs = Utilisateur.findAllByActif(true)
		return [commandeInstance: commandeInstance, entrees: entrees, plats: plats, desserts: desserts, utilisateurs: utilisateurs]
	}
	
	def save = {
		def commandeInstance = new Commande(params)
		
		
		if (commandeInstance.save(flush: true)) {
			flash.message = "${message(code: 'default.created.message', args: [message(code: 'commande.label', default: 'Commande'), commandeInstance.id])}"
			redirect(action: "show", id: commandeInstance.id)
		}
		else {
			render(view: "create", model: [commandeInstance: commandeInstance])
		}
	}
	
	def show = {
		def commandeInstance = Commande.get(params.id)
		if (!commandeInstance) {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'commande.label', default: 'Commande'), params.id])}"
			redirect(action: "list")
		}
		else {
			[commandeInstance: commandeInstance]
		}
	}
	
	def edit = {
		def commandeInstance = Commande.get(params.id)
		if (!commandeInstance) {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'commande.label', default: 'Commande'), params.id])}"
			redirect(action: "list")
		}
		else {
			return [commandeInstance: commandeInstance]
		}
	}
	
	def update = {
		def commandeInstance = Commande.get(params.id)
		
		if (commandeInstance) {
			if (params.version) {
				def version = params.version.toLong()
				if (commandeInstance.version > version) {
					
					commandeInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'commande.label', default: 'Commande')] as Object[], "Another user has updated this Commande while you were editing")
					render(view: "edit", model: [commandeInstance: commandeInstance])
					return
				}
			}
			commandeInstance.properties = params
			
			if (!commandeInstance.hasErrors() && commandeInstance.save(flush: true)) {
				flash.message = "${message(code: 'default.updated.message', args: [message(code: 'commande.label', default: 'Commande'), commandeInstance.id])}"
				redirect(action: "show", id: commandeInstance.id)
			}
			else {
				render(view: "edit", model: [commandeInstance: commandeInstance])
			}
		}
		else {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'commande.label', default: 'Commande'), params.id])}"
			redirect(action: "list")
		}
	}
	
	def delete = {
		def commandeInstance = Commande.get(params.id)
		if (commandeInstance) {
			try {
				commandeInstance.delete(flush: true)
				flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'commande.label', default: 'Commande'), params.id])}"
				redirect(action: "list")
			}
			catch (org.springframework.dao.DataIntegrityViolationException e) {
				flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'commande.label', default: 'Commande'), params.id])}"
				redirect(action: "show", id: params.id)
			}
		}
		else {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'commande.label', default: 'Commande'), params.id])}"
			redirect(action: "list")
		}
	}
}
